package client;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;

import entry.Entry;
import event.Event;
import event.EventHandler;

public class ServerSearchResultEventHandler implements EventHandler {

	private final ClientController mController;

	public ServerSearchResultEventHandler(ClientController controller) {
		mController = controller;
	}

	@Override
	public void handle(Event e) {
		if (e.type == Event.DIR_TYPE) {
			byte[] message = e.message;

			try {
				ObjectInputStream is = new ObjectInputStream(
						new ByteArrayInputStream(message));

				final ArrayList<Entry> results = (ArrayList<Entry>) is
						.readObject();

				if (results != null) {
					System.out.println("> Start receive search results: "
							+ e.parameters);

					mController.getRemoteDirectory().addAll(results);

					System.out.println("> End receive search results: "
							+ e.parameters);
				}
			} catch (IOException ex) {
				// TODO Auto-generated catch block
				ex.printStackTrace();
			} catch (ClassNotFoundException ex) {
				// TODO Auto-generated catch block
				ex.printStackTrace();
			} finally {

			}
		}
	}
}
